package com.rocky.flutter.tuya.tuya;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSON;
import com.tuya.smart.android.ble.api.LeScanSetting;
import com.tuya.smart.android.ble.api.ScanType;
import com.tuya.smart.home.sdk.TuyaHomeSdk;
import com.tuya.smart.home.sdk.builder.TuyaQRCodeActivatorBuilder;
import com.tuya.smart.sdk.api.IResultCallback;
import com.tuya.smart.sdk.api.ITuyaActivator;
import com.tuya.smart.sdk.api.ITuyaDevice;
import com.tuya.smart.sdk.api.ITuyaSmartActivatorListener;
import com.tuya.smart.sdk.bean.DeviceBean;

import io.flutter.plugin.common.MethodChannel;

public class TuyaDevice {
    // You need to check permissions before using Bluetooth devices

    public static void StartScanDevice(MethodChannel.Result result) throws Exception {
        // Scan Single Ble Device
        LeScanSetting scanSetting = new LeScanSetting.Builder()
                .setTimeout(60 * 1000) // Timeout：ms
                .addScanType(ScanType.SINGLE) // If you need to scan for BLE devices, you only need to add ScanType.SINGLE
                .addScanType(ScanType.SINGLE_QR)
                .build();

        // start scan
        TuyaHomeSdk.getBleOperator().startLeScan(scanSetting, bean -> {
            result.success(JSON.toJSONString(bean));
        });
    }

    public static void stopScanDevice(MethodChannel.Result result){
        TuyaHomeSdk.getBleOperator().stopLeScan();
        result.success(true);
    }

    public static void activeDevice(long homeId, String uuid, MethodChannel.Result result) throws Exception {
        Application mApplication = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null,(Object[])null);
        TuyaQRCodeActivatorBuilder builder = new TuyaQRCodeActivatorBuilder()
                .setUuid(uuid)
                .setHomeId(homeId)
                .setContext(mApplication.getApplicationContext())
                .setTimeOut(30000)
                .setListener(new ITuyaSmartActivatorListener() {

                                 @Override
                                 public void onError(String errorCode, String errorMsg) {
                                     result.error(errorCode,errorMsg, null);
                                 }

                                 @Override
                                 public void onActiveSuccess(DeviceBean devResp) {
                                     result.success(JSON.toJSONString(devResp));
                                 }

                                 @Override
                                 public void onStep(String step, Object data) {
                                     System.out.println(step);
                                 }
                             }
                );

        ITuyaActivator mTuyaActivator = TuyaHomeSdk.getActivatorInstance().newQRCodeDevActivator(builder);
//开始配网
        mTuyaActivator.start();

    }

    public static void resetFactory(String deviceId,MethodChannel.Result result){
        ITuyaDevice mDevice = TuyaHomeSdk.newDeviceInstance(deviceId);
        mDevice.resetFactory(new IResultCallback() {
            @Override
            public void onError(String errorCode, String errorMsg) {
                result.error(errorCode,errorMsg, null);
            }

            @Override
            public void onSuccess() {
                result.success(true);
            }
        });
    }

    public static void removeDevice(String deviceId,MethodChannel.Result result){
        ITuyaDevice mDevice = TuyaHomeSdk.newDeviceInstance(deviceId);
        mDevice.removeDevice(new IResultCallback() {
            @Override
            public void onError(String errorCode, String errorMsg) {
                result.error(errorCode,errorMsg, null);
            }

            @Override
            public void onSuccess() {
                result.success(true);
            }
        });
    }
}
